﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Bat : MonsterBase {



    public override void Init()
    {
        base.Init();
        flyspeed = MoveSpeed;
    }


    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.gameObject.layer == 9)
        {
            Hurt(10);
        }
    }
    #region 移动，其实是飞行


    private float flyspeed;
    /// <summary>
    /// 移动是随机移动到目标附近的某个位置，到达该位置后再重新选取目标点
    /// </summary>
    private void EnableGhostShadow(bool active)
    {
        if (GetComponent<GhostShadow>())
        {
            GetComponent<GhostShadow>().enable = active;
        }
    }

    //public override void UpdateFunc()
    //{
    //    base.UpdateFunc();

    //}

    public override void Move()
    {
        if ((transform.position - Target.transform.position).magnitude < 4)
        {
            MoveSpeed = flyspeed * 2;
            EnableGhostShadow(true);
        }
        else
        {
            MoveSpeed = flyspeed;
            EnableGhostShadow(false);
        }

        if (AttachTargetPosition())
        {
            GetRandomPosition();
        }
        transform.Translate((targetPos-(Vector2)transform.position).normalized*Time.deltaTime*MoveSpeed, Space.World);
    }
    private Vector2 targetPos=new Vector2(0,0);
    private void GetRandomPosition()
    {
        targetPos = (Vector2)Target.transform.position + new Vector2(
            Random.Range(-1.8f,1.8f),
            Random.Range(-1.8f, 1.8f));
    }
    /// <summary>
    /// 
    /// </summary>
    /// <returns></returns>
    private bool AttachTargetPosition()
    {
        if ((targetPos - (Vector2)transform.position).magnitude < 0.1)
        {
            return true;
        }
        return false;
    }
    #endregion
}
